//-----------------------------------------------------------------------
// <copyright file="TypeCompanyDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class TypeCompanyDAOBase
    {
        #region Common methods
        public virtual TypeCompany CreateTypeCompanyFromReader(IDataReader reader)
        {
            TypeCompany item = new TypeCompany();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("TypeCompanyID"))) item.TypeCompanyID = (int)reader["TypeCompanyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (int)reader["SortOrder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTypeCompanyFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateTypeCompany methods
            
        public virtual void CreateTypeCompany(TypeCompany typeCompany)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, typeCompany.Title);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int32, typeCompany.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, typeCompany.Enable);
                database.AddOutParameter(dbCommand, "@TypeCompanyID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                typeCompany.TypeCompanyID = (int)database.GetParameterValue(dbCommand, "@TypeCompanyID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTypeCompanyException, ex);
            }
        }

        #endregion

        #region UpdateTypeCompany methods
        
        public virtual void UpdateTypeCompany(TypeCompany typeCompany)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyUpdate");            
                
                database.AddInParameter(dbCommand, "@TypeCompanyID", DbType.Int32, typeCompany.TypeCompanyID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, typeCompany.Title);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int32, typeCompany.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, typeCompany.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateTypeCompanyException, ex);
            }
        }
        
        #endregion

        #region DeleteTypeCompany methods
        public virtual void DeleteTypeCompany(int typeCompanyID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyDelete");
                
                database.AddInParameter(dbCommand, "@TypeCompanyID", DbType.Int32, typeCompanyID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteTypeCompanyException, ex);
            }
        }

        #endregion

        #region GetTypeCompany methods
        
        public virtual TypeCompany GetTypeCompany(int typeCompanyID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyGet");
                
                database.AddInParameter(dbCommand, "@TypeCompanyID", DbType.Int32, typeCompanyID);
                
                TypeCompany typeCompany =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        typeCompany = CreateTypeCompanyFromReader(reader);
                        reader.Close();
                    }
                }
                return typeCompany;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTypeCompanyException, ex);
            }
        }
        
        #endregion

        #region GetTypeCompanyList methods
        public virtual TypeCompanyCollection GetTypeCompanyList(TypeCompanyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TypeCompanyCollection typeCompanyCollection = new TypeCompanyCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        TypeCompany typeCompany = CreateTypeCompanyFromReader(reader);
                        typeCompanyCollection.Add(typeCompany);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return typeCompanyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTypeCompanyListException, ex);
            }
        }
        
        public virtual TypeCompanyCollection GetTypeCompanyList(TypeCompanyColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetTypeCompanyList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetTypeCompanyList methods
        public virtual TypeCompanyCollection GetTypeCompanyListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTypeCompanyGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TypeCompanyCollection typeCompanyCollection = new TypeCompanyCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        TypeCompany typeCompany = CreateTypeCompanyFromReader(reader);
                        typeCompanyCollection.Add(typeCompany);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return typeCompanyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTypeCompanyListException, ex);
            }
        }
        
        public virtual TypeCompanyCollection GetTypeCompanyListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetTypeCompanyListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}